package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author zsk
 * @create 2022-02-15-16:29
 */
@RestController
public class DishController {

    //依赖注入
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /*
    显示数据，查询所有,分页查询
    Request URL: http://localhost:8080/dish/page?page=1&pageSize=10
    Request Method: GET
     */
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,//当前页为1
            @RequestParam(defaultValue = "10") Integer pageSize,//每页条数为10
            String name//有可能会根据姓名查询
    ){

        //调用service层查询
        Page<Dish> page = dishService.findByPage(pageNum,pageSize,name);

        //返回结果
        return ResultInfo.success(page);

    }

    /*
    新建菜品回显数据,回显分类数据
    Request URL: http://localhost:8080/category/list?type=1
    Request Method: GET
     */
    @GetMapping("/category/list")
    public ResultInfo findList(Integer type){

        //调用service层查询
        List<Category> categoryList = categoryService.findList(type);

        //返回结果
        return ResultInfo.success(categoryList);
    }

    /*
    修改前回显数据
    Request URL: http://localhost:8080/dish/1493536374061203458
    Request Method: GET
     */
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){

        //调用service层查询,返回一条菜品数据
        Dish dish = dishService.findById(id);

        //返回数据
        return ResultInfo.success(dish);
    }


    //===========从这往下添加Redis操作============\\

    /*
    新建菜品
    Request URL: http://localhost:8080/dish
    Request Method: POST
     */
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){

        //当我们新增菜品的时候，会更改菜品分类列表里的数据，所以先根据key删除对应的数据
        String key = "dish_" + dish.getCategoryId();
        redisTemplate.delete(key);

        //调用service层添加
        dishService.save(dish);
        //返回结果
        return ResultInfo.success(null);

    }

    /*
    修改菜品信息
    Request URL: http://localhost:8080/dish
    Request Method: PUT
     */
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){

        //修改菜品也会更改Redis里的数据信息，所以先删除(删除所有的菜品分类)
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        //调用service层修改
        dishService.update(dish);
        //返回结果
        return ResultInfo.success(null);

    }

    /*
    删除数据
    Request URL: http://localhost:8080/dish?ids=1493536374061203458
    Request Method: DELETE
     */
    @DeleteMapping("/dish")
    public ResultInfo deleteById(Long[] ids){

        //删除Redis中的所有菜品信息
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        //掉用service层删除
        dishService.deleteById(ids);
        return ResultInfo.success(null);
    }


    /*
    批量启售/禁售
    Request URL: http://localhost:8080/dish/status/0?ids=1493536374061203458
    Request Method: POST
     */
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,
                                   long[] ids){
        //调用service,获取所有的keys
        Set keys = redisTemplate.keys("dish_*");
        //根据keys删除Redis中的值
        redisTemplate.delete(keys);

        dishService.updateStatus(status,ids);
        //返回结果
        return ResultInfo.success(null);
    }
}
